Lecture Lab 9

Søren Helweg Dam

Creating a Simple Shiny App

Lab 9 Learning Objectives

A student who has met the objectives of the session will be able to:

  • Prepare a simple shiny application

  • Using relevant online resources to autonomously identify and obtain new and expand on existing knowledge of R

Today’s lab

  • The two main components of a web application
  • The difference between imperative and declarative programming
  • Using golem and modules for shiny app development
  • How to create an R shiny app

Question

What are the two main components of a web application?

Question

What are the two main components of a web application?

  • User Interface (Front end)

  • Server (Back end)

The simplest Shiny App

library(shiny)

# User Interface (Front end)
ui <- fluidPage(
  "Hello World!"
  )

# Server (Back end)
server <- function(input, output, session){}

# Running the app
shinyApp(ui, server)

What is user interface?

  • “The user interface (UI) is the point at which human users interact with a computer, website or application.

  • “The goal of effective UI is to make the user’s experience easy and intuitive, requiring minimum effort on the user’s part to receive maximum desired outcome.”








- source

User Interface

  • When you know the purpose of your app, the first place to start is to think about the app layout.

  • For inspiration either use:

  • the Shiny Layout Guide

  • Or the Shiny Cheat Sheet

Shiny Inputs

UI take-aways

  • Start with layout
  • Define/create your inputs
  • Use online resource for help and inspiration

UI take-aways

  • Start with layout
  • Define/create your inputs
  • Use online resource for help and inspiration


  • Now on to some programming

Imperative vs declarative programming

  • Imperative: Issued commands are executed immediately

  • Declarative: You set a high-level goal or describe a set of constraints to be translated into actions later

  • “With imperative code you say ‘Make me a sandwich’.”

  • “With declarative code you say ‘Ensure there is a sandwich in the refrigerator whenever I look inside of it’”






- source

Shiny is declarative

  • Shiny translates your declared goals and constraints only when necessary.

  • What are the benefits and issues with declarative programming?

    • You can have many actions ready, but only some are used
    • You do not need to define when the action should take place
    • But, initiating the action is strongly dependent on the name of the declaration










- source

Reactivity

Questions?

Introducing golem

  • A framework for building production-grade shiny applications.
  • It relies on the idea that every shiny application should be built as an R package
  • But what does that mean and why?









- source

Why golem?

  • It has metadata
  • It handles dependencies
  • It’s split into functions
  • It has documentation
  • It’s tested
  • There is a native way to build and deploy it






- source

Shiny modules

  • Breaks the application into bite size pieces

  • Helps you have a mental model of the application

  • But isn’t it complicated?

  • Let me show you..

Questions?

Break, then exercises!